package com.sheliduan.itlearning.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sheliduan.itlearning.admin.model.vos.user.DeleteUserReqVO;
import com.sheliduan.itlearning.admin.model.vos.user.FindUserPageListReqVO;
import com.sheliduan.itlearning.admin.model.vos.user.UpdateUserReqVO;
import com.sheliduan.itlearning.admin.service.AdminUserService;
import com.sheliduan.itlearning.common.domain.dos.UserDO;
import com.sheliduan.itlearning.common.domain.mapper.UserMapper;
import com.sheliduan.itlearning.common.utils.PageResponseData;
import com.sheliduan.itlearning.common.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 断舍离
 * @date: 2025/2/13 9:45
 * @description: 后台管理员用户Service实现类
 **/
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResponseData findUserPageList(FindUserPageListReqVO reqVO) {
        long current = reqVO.getCurrent();
        long size = reqVO.getSize();
        String role = reqVO.getRole();
        String username = reqVO.getUsername();
        LocalDateTime startDate = reqVO.getStartDate();
        LocalDateTime endDate = reqVO.getEndDate();
        // MyBatis分页对象(查询第几页,每页多少条数据,分页查询条件)
        Page<UserDO> page = userMapper.selectPageList(current, size, role, username, startDate, endDate);
        // 根据分页对象转为userDO集合
        List<UserDO> userDOS = page.getRecords();
        return PageResponseData.success(page, userDOS);
    }

    @Override
    public ResponseData updateUser(UpdateUserReqVO reqVO) {
        int i = userMapper.updateUser(reqVO.getId(), reqVO.getUsername(), reqVO.getAvatar(), reqVO.getPassword());
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("修改用户失败");
    }

    @Override
    public ResponseData deleteUser(DeleteUserReqVO reqVO) {
        int i = userMapper.deleteById(reqVO.getId());
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("删除用户失败");
    }

    @Override
    public ResponseData selectById(Integer id) {
        UserDO userDO = userMapper.selectById(id);
        if(userDO != null)
            return ResponseData.success(userDO);
        else
            return ResponseData.fail("用户不存在");
    }
}
